package xyz.weizhi.rjwm.service.Imp;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import xyz.weizhi.rjwm.common.CustomException;
import xyz.weizhi.rjwm.entity.Setmeal;
import xyz.weizhi.rjwm.entity.SetmealDish;
import xyz.weizhi.rjwm.entity.vo.SetmealDto;
import xyz.weizhi.rjwm.mapper.SetmealDao;
import xyz.weizhi.rjwm.service.SetmealDishService;
import xyz.weizhi.rjwm.service.SetmealService;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author milian
 * @Date 2021/9/25 0025 14:38
 */
@Service
@Slf4j
public class SetmealServiceImp extends ServiceImpl<SetmealDao, Setmeal> implements SetmealService {
    @Autowired
    private SetmealDishService setmealDishService;
    /**
     * @Author milian
     * @Description //保存套餐信息
     * @Date 8:46 2021/9/28 0028
     * @Param [setmealDto]
     * @return boolean
     **/
    @Transactional
    public boolean saveWithDish(SetmealDto setmealDto) {
        boolean save = this.save(setmealDto);
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        setmealDishes = setmealDishes.stream().map(item -> {
            item.setSetmealId(setmealDto.getId());
            return item;
        }).collect(Collectors.toList());
        boolean b = setmealDishService.saveBatch(setmealDishes);
        return save&&b;
    }

    /**
     * @Author milian
     * @Description //查询套餐信息
     * @Date 8:46 2021/9/28 0028
     * @Param [id]
     * @return xyz.weizhi.rjwm.entity.vo.SetmealDto
     **/
    public SetmealDto queryWithDish(long id) {
        SetmealDto setmealDto = new SetmealDto();
        LambdaQueryWrapper<SetmealDish> lqw = new LambdaQueryWrapper<>();
        Setmeal Setmeal = this.getById(id);
        lqw.eq(SetmealDish::getSetmealId,id);
        List<SetmealDish> list = setmealDishService.list(lqw);
        setmealDto.setSetmealDishes(list);
        BeanUtils.copyProperties(Setmeal,setmealDto);
        return setmealDto;
    }
    /**
     * @Author milian
     * @Description //更新套餐信息
     * @Date 8:58 2021/9/28 0028
     * @Param [setmealDto]
     * @return boolean
     **/
    @Transactional
    @Override
    public boolean updateWithDish(SetmealDto setmealDto) {
        log.info("setmealDto:{}",setmealDto);
        boolean b = this.updateById(setmealDto);
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        setmealDishes=setmealDishes.stream().map(item->{
            item.setSetmealId(setmealDto.getId());
            return item;
        }).collect(Collectors.toList());
        boolean b1 = setmealDishService.updateBatchById(setmealDishes);
        return b&&b1;
    }
    @Transactional
    @Override
    public boolean updateWithDishByStatus(long[] ids, Integer status) {
        try {
            for (long id : ids) {
                LambdaUpdateWrapper<Setmeal> uw = new LambdaUpdateWrapper<>();
                uw.eq(Setmeal::getId,id);
                uw.set(Setmeal::getStatus,status);
                this.update(null, uw);
            }
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }
    @Transactional
    @Override
    public boolean deleteWithDish(List<Long> ids) {
        //查询套餐状态，确定是否可用删除
        LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper();
        queryWrapper.in(Setmeal::getId,ids);
        queryWrapper.eq(Setmeal::getStatus,1);

        int count = this.count(queryWrapper);
        if(count > 0){
            //如果不能删除，抛出一个业务异常
            throw new CustomException("套餐正在售卖中，不能删除");
        }

        //如果可以删除，先删除套餐表中的数据---setmeal
        boolean b = this.removeByIds(ids);

        //delete from setmeal_dish where setmeal_id in (1,2,3)
        LambdaQueryWrapper<SetmealDish> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.in(SetmealDish::getSetmealId,ids);
        //删除关系表中的数据----setmeal_dish
        setmealDishService.remove(lambdaQueryWrapper);
        return b;
    }
}
